<template>
  <div class="app-container">
    <!-- 添加或修改mqtt配置对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="端口" prop="port">
		  <el-select v-model="form.port" placeholder="请输入端口" clearable size="small">
		    <el-option
		      v-for="dict in list"
		      :key="dict"
		      :label="dict"
		      :value="dict"
		    />
		  </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">连接</el-button>
        <el-button type="primary" @click="close">关闭</el-button>
      </div>
  </div>
</template>

<script>
import { serialPortConnect, getSerialPorttConfig,getSerialPortAll,serialPortClose} from "@/api/tranShort/serialPorttConfig";

export default {
  name: "MqttConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // mqtt配置表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ip: null,
        username: null,
        password: null,
        port: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
	  this.getSerialPortAll()
    this.getInfo();
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ip: null,
        username: null,
        password: null,
        port: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 连接按钮操作 */
    getInfo() {
      this.reset();
      getSerialPorttConfig().then(response => {
        this.form = response.data;
      });
    },
	// 获取所有
	getSerialPortAll(){
		getSerialPortAll().then(res=>{
			if(res.code==200){
				this.list = res.data;
			}else{
				this.$message.error(res.msg)
			}
		}).catch(err=>{
			console.log(err)
		})
		
	},
    /** 连接按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
			  
			  let {id,port} = this.form;
			  
              serialPortConnect({id,port}).then(response => {
              this.msgSuccess("连接成功");
              this.getInfo();
            });
          }
        }
      });
    },
    /**close*/
    close(){
        this.$refs["form"].validate(valid => {
            if (valid) {
                serialPortClose().then(response => {
                    this.msgSuccess("关闭成功");
                    this.getInfo();
                });
            }
        });

    },

  }
};
</script>
